package org.sbbs.admin.mbean.sysrole;

import org.sbbs.admin.mbean.sysparam.*;
import jakarta.faces.application.FacesMessage;
import static jakarta.faces.application.FacesMessage.SEVERITY_WARN;
import jakarta.faces.component.UIComponent;
import jakarta.faces.context.FacesContext;
import jakarta.faces.validator.ValidatorException;
import jakarta.inject.Named;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.omnifaces.cdi.ViewScoped;
import org.sbbs.admin.dao.SysRoleRepo;
import org.sbbs.admin.entity.SysRole;
import org.sbbs.admin.manager.SysRoleManager;
import org.sbbs.base.jsf.BaseTableMBean;
import static org.sbbs.base.jsf.BaseTableMBean.addDetailMessage;

/**
 *
 * @author jenkins
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Named("sysRoleMB")
//@Component("sysRoleMB")
@ViewScoped
//@SessionScoped
public class SysRoleMBean extends BaseTableMBean<SysRole, Long, SysRoleRepo, SysRoleManager> {

    private static final long serialVersionUID = -8170966700137807744L;

    public SysRoleMBean(SysRoleManager m) {
        super(m);
    }

    @Override
    public SysRole createNewObj() {
        return new SysRole();
    }

    public void showMessage(Object msg) {
        addDetailMessage("fdc", SEVERITY_WARN);
    }

    public void keyUniqValidate(FacesContext context, UIComponent component, Object value) {

        String key = (String) value;
        SysRole sp = this.getManager().getByKey(key);

        if (sp != null && !sp.getId().equals(this.getEditObj().getId())) {
            FacesMessage fms = new FacesMessage(this.getSma().getMessage("sys.role.key.uniq"));
            fms.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(fms);
        }
    }
}
